using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Atechnology.glassCAD.Shapes.CutViewer
{
    public class CutModel : IGeometry
    {
        private bool drawBoundingBox = false;

        //private Pen arcPen = Pens.Black;

        //public Pen ArcPen
        //{
        //    get { return arcPen; }
        //    set { arcPen = value; }
        //}
        //private Pen linePen = Pens.Black;

        //public Pen LinePen
        //{
        //    get { return linePen; }
        //    set { linePen = value; }
        //}
        //private Pen tipPen = Pens.Blue;

        //public Pen TipPen
        //{
        //    get { return tipPen; }
        //    set { tipPen = value; }
        //}       

        private Tip tip;

        private List<IGeometry> figures;

        public List<IGeometry> Figures
        {
            get { return figures; }
            set { figures = value; }
        }

        public CutModel()
        {
            tip = new Tip();
            tip.Enabled = false;
            figures = new List<IGeometry>();
        }

        #region IFigure Members

        public virtual void Draw(System.Drawing.Graphics g, RectangleF modelBoundingBox, int screenWidth, int screenHeight, IDrawVisitor drawVisitor)
        {
            drawVisitor.Visit(this, g, modelBoundingBox, screenWidth, screenHeight);

            //Pen currentPen = null;
            //foreach (IGeometry figure in this.figures)
            //{
            //    figure.Tip.TipPen = Pens.Gray;
            //    if (figure is Line)
            //        currentPen = linePen;
            //    if (figure is Arc)
            //        currentPen = arcPen;
            //    if(pen != null)
            //        figure.Draw(g, modelBoundingBox, screenWidth, screenHeight, pen);
            //    else
            //        figure.Draw(g, modelBoundingBox, screenWidth, screenHeight, currentPen);
            //}
        }

        #endregion

        #region IGeometry Members


        public virtual System.Drawing.RectangleF BoundingBox
        {
            get
            {
                float minX = float.MaxValue, maxX = float.MinValue;
                float minY = float.MaxValue, maxY = float.MinValue;

                foreach (IGeometry geom in this.figures)
                {
                    if (!(geom is Tip))
                    {
                        if (minX > geom.BoundingBox.X)
                            minX = geom.BoundingBox.X;
                        if (maxX < geom.BoundingBox.X + geom.BoundingBox.Width)
                            maxX = geom.BoundingBox.X + geom.BoundingBox.Width;
                        if (minY > geom.BoundingBox.Y)
                            minY = geom.BoundingBox.Y;
                        if (maxY < geom.BoundingBox.Y + geom.BoundingBox.Height)
                            maxY = geom.BoundingBox.Y + geom.BoundingBox.Height;                        
                    }
                }

                //foreach (IGeometry line in this.figures)
                //{
                //    if(line is Line)
                //        foreach (PointF linePoint in ((Line)line).Points)
                //        {
                //            if (minX > linePoint.X) minX = linePoint.X;
                //            if (maxX < linePoint.X) maxX = linePoint.X;
                //            if (minY > linePoint.Y) minY = linePoint.Y;
                //            if (maxY < linePoint.Y) maxY = linePoint.Y;                        
                //        }
                //}

                return new RectangleF(minX, minY, maxX - minX, maxY - minY);
            }
        }

        #endregion

        #region IGeometry Members


        public void Update()
        {
            foreach (IGeometry geom in this.figures)
            {
                geom.Update();
            }
        }

        public Tip Tip
        {
            get { return tip; }
        }

        #endregion

        #region IGeometry Members


        public bool DrawBoundingBox
        {
            get
            {
                return drawBoundingBox;
            }
            set
            {
                drawBoundingBox = value;
                foreach(IGeometry geom in this.figures)
                {
                    if (geom is Tip)
                        continue;
                    geom.DrawBoundingBox = value;
                }
            }
        }

        #endregion
    }
}
